<script setup>
import topNav from "../../components/topNav.vue";
import { introApi } from "@/services/index";
import { onLoad, onShow } from "@dcloudio/uni-app";
import { nextTick, ref, watch, onMounted } from "vue";
import { appInfo, loginSwitch } from "@/services/user";
// import topNav from "../../components/topNav.vue";
import { operation } from "@/utils/num.js";
import {
  appScTransferDetail,
  StorePurchaseDetail,
  scTransferSign,
  scTransferRemind,
  appScTransferReturnQuery,
} from "@/services/transfer";

const showSelect = ref("");

if (uni.getStorageSync("selectShopType")) {
  showSelect.value = uni.getStorageSync("selectShopType");
}

const id = ref("");
const type = ref(1);
onLoad((e) => {
  id.value = e.id;
  type.value = e.type ? e.type : 1;

  // getAppScTransferReturnQuery();
});
onShow(() => {
  getAppScTransferDetail();
  // getAppScTransferReturnQuery();
});

const optionDet = ref({});
const returnList = ref([]);

const getAppScTransferDetail = async () => {
  try {
    let res = await StorePurchaseDetail({ id: id.value });
    optionDet.value = res.data;
  } catch (e) {}
};

const getAppScTransferReturnQuery = async () => {
  try {
    let res = await appScTransferReturnQuery({ id: id.value });
    returnList.value = res.data;
  } catch (e) {}
};

const toReturn = () => {
  uni.navigateTo({
    url: "/pages/transfer/return/index?id=" + id.value,
  });
};

//签收

const passwordShow = ref(false);
const isshowpassword = ref(false);
// const selectItem = ref({});
const password = ref("");

const showQs = () => {
  passwordShow.value = true;
  isshowpassword.value = false;
  password.value = "";
  // selectItem.value = item;
};
const isUp = ref(false);
const qrUpSign = async () => {
  if (!password.value) {
    uni.showToast({
      title: "请输入登录密码",
      icon: "none",
    });
    return;
  }

  if (isUp.value) {
    return;
  }
  isUp.value = true;
  try {
    let res = await scTransferSign({
      id: id.value,
      password: password.value,
    });
    if (res.code == 200) {
      uni.showToast({
        title: "签收成功",
        icon: "none",
      });
      optionDet.value.status = 12;
      isUp.value = false;
      passwordShow.value = false;
      // getAppProductQuery(1);
    } else {
      uni.showToast({
        title: res.msg,
        icon: "none",
      });
      isUp.value = false;
    }

    console.log(res);
  } catch (e) {
    uni.showToast({
      title: e.msg,
      icon: "none",
    });
    isUp.value = false;
  }
};

const toSign = async () => {
  try {
    let res = await scTransferRemind({
      id: id.value,
    });
    if (res.code == 200) {
      uni.showToast({
        title: "您已发送提醒",
        icon: "success",
      });

      // getAppProductQuery(1);
    } else {
      uni.showToast({
        title: res.msg,
        icon: "none",
      });
    }

    console.log(res);
  } catch (e) {
    uni.showToast({
      title: e.msg,
      icon: "none",
    });
  }
};

const previewImage = (urlls, index) => {
  uni.previewImage({
    current: index, // 当前显示图片的http链接
    urls: urlls, // 需要预览的图片http链接列表
    longPressActions: {
      itemList: ["发送给朋友", "保存图片", "收藏"],
      success: function (data) {
        console.log(
          "选中了第" +
            (data.tapIndex + 1) +
            "个按钮,第" +
            (data.index + 1) +
            "张图片"
        );
      },
      fail: function (err) {
        console.log(err.errMsg);
      },
    },
  });
};

//无网状态
const connected = () => {
  getAppScTransferDetail();
};

//提醒收货
</script>

<template>
  <view class="bg">
    <topNav
      :title="'自采入库详情'"
      @save="saveFun"
      :border="true"
      rType="save"
    ></topNav>

    <view class="top">
      <!-- <view class="top_title">
        <text
          >{{ optionDet.sourceScName }} 调至 {{ optionDet.targetScName }}</text
        >
      </view> -->
      <view class="top_dn">单据编号：{{ optionDet.id }}</view>
      <view class="top_dn">入库：{{ optionDet.scName }}</view>
      <view class="top_dn"
        >{{ optionDet.createTime }} {{ optionDet.purchaserName }}</view
      >
    </view>

    <view class="info">
      <view class="info_title">入库明细</view>
      <!-- <view class="info_dh">
        <text></text>
        <text><text class="color">共</text>{{ optionDet.totalNum }}件</text>
      </view> -->
      <view class="info_list">
        <view class="info_item title">
          <view class="info_item_title">商品名称</view>
          <view class="info_item_title">商品条码</view>
          <view class="info_item_num">数量</view>
          <view class="info_item_uit">单位</view>
          <!-- <view class="info_item_price">调货价</view> -->
        </view>
        <view
          class="info_item texts"
          v-for="(item, index) in optionDet.products"
          :key="index"
        >
          <view class="info_item_title"
            ><text>{{ item.name }}</text>
          </view>
          <view class="info_item_title"
            ><text>{{ item.barCode ? item.barCode : "--" }}</text>
          </view>
          <view class="info_item_num">{{ item.purchaseNum }}</view>
          <view class="info_item_uit">{{ item.unit }}</view>
          <!-- <view class="info_item_price">{{
            item.taxPrice ? item.taxPrice : "--"
          }}</view> -->
        </view>
      </view>
      <view class="info_item-fo">
        <view class="info_item_left">共 {{ optionDet.totalNum }} 件</view>
        <!-- <view class="info_item_center">入库总价:</view>
        <view class="info_item_right">¥{{ optionDet.totalAmount }}</view> -->
      </view>
    </view>

    <view class="info" v-if="returnList.length > 0">
      <view class="info_title">调货退货明细</view>
      <view v-for="(item, index) in returnList" :key="index">
        <view class="info_dh">
          <text><text class="color">退款人:</text> {{ item.approveBy }} </text>
          <text><text class="color">时间:</text>{{ item.updateTime }}</text>
        </view>
        <view class="info_list">
          <view class="info_item title">
            <view class="info_item_title">商品名称</view>
            <view class="info_item_num">数量</view>
            <view class="info_item_uit">单位</view>
            <!-- <view class="info_item_price">调货价</view> -->
          </view>
          <view
            class="info_item texts"
            v-for="(itemGoods, indexGoods) in item.details"
            :key="indexGoods"
          >
            <view class="info_item_title"
              ><text>{{ itemGoods.categoryName }}</text>
            </view>
            <view class="info_item_num">{{ itemGoods.returnNum }}</view>
            <view class="info_item_uit">{{ itemGoods.unit }}</view>
            <!-- <view class="info_item_price">{{ itemGoods.taxPrice }}</view> -->
          </view>
        </view>
        <view class="info_item-fo">
          <view class="info_item_left">共 {{ item.totalNum }} 件</view>
          <!-- <view class="info_item_center">入库总价:</view>
          <view class="info_item_right bt-colors">¥{{ item.totalAmount }}</view> -->
        </view>
      </view>
    </view>
    <view class="bt_pz">
      <view class="bt_pz_title">凭证</view>
      <view class="bt_pz_img">
        <image
          v-for="(item, index) in optionDet.purchaseCertificate"
          :key="index"
          :src="item"
          mode="apectFill"
          @click="previewImage(optionDet.purchaseCertificate, index)"
        />
      </view>
      <view class="bt_pz_title bt_pz_title2">
        <text>备注</text>

        <text class="bt_pz_title2_text">{{
          optionDet.description || "--"
        }}</text>
      </view>
      <!-- <view class="bt_pz_img2">
        <view class="bt_pz_img_text">{{ optionDet.description }}</view>
      </view> -->
    </view>
    <view style="width: 100%; height: 150rpx"></view>
  </view>
  <!-- v-if="type == 1" -->

  <view
    class="bt_form"
    v-permission="[
      'manager:scTransfer:remind',
      'manager:scTransfer:return:create',
    ]"
    v-if="
      showSelect == 'admin' &&
      type == 1 &&
      (optionDet.status == 3 ||
        (optionDet.status == 12 &&
          optionDet.totalNum - optionDet.totalReturnNum > 0))
    "
  >
    <view class="bt_form_k">
      <up-button
        v-permission="['manager:scTransfer:remind']"
        v-if="optionDet.status == 3"
        class="bt_close"
        type="default"
        :plain="true"
        :hairline="true"
        text="提醒签收"
        @click="toSign"
      ></up-button>
      <!-- v-permission="['manager:scTransfer:return:create']" -->
      <up-button
        v-if="
          optionDet.status == 12 &&
          optionDet.totalNum - optionDet.totalReturnNum > 0
        "
        v-permission="['manager:scTransfer:return:create']"
        class="bt_up"
        type="default"
        :plain="true"
        :hairline="true"
        text="调货退货"
        @click="toReturn"
      ></up-button>
    </view>
  </view>
  <view
    class="bt_form"
    v-permission="['manager:scTransfer:sign']"
    v-if="showSelect == 'admin' && type == 2 && optionDet.status == 3"
  >
    <view class="bt_form_k">
      <up-button
        v-if="optionDet.status == 3"
        class="bt_up bt_bl"
        :plain="true"
        :hairline="true"
        text="确认签收"
        @click="showQs"
      ></up-button>
    </view>
  </view>

  <view
    class="bt_form"
    v-permission="['shop:scTransfer:remind', 'shop:scTransfer:return:create']"
    v-if="
      showSelect == 'shop' &&
      type == 1 &&
      (optionDet.status == 3 ||
        (optionDet.status == 12 &&
          optionDet.totalNum - optionDet.totalReturnNum > 0))
    "
  >
    <view class="bt_form_k">
      <up-button
        v-permission="['shop:scTransfer:remind']"
        v-if="optionDet.status == 3"
        class="bt_close"
        type="default"
        :plain="true"
        :hairline="true"
        text="提醒签收"
        @click="toSign"
      ></up-button>
      <up-button
        v-permission="['shop:scTransfer:return:create']"
        v-if="
          optionDet.status == 12 &&
          optionDet.totalNum - optionDet.totalReturnNum > 0
        "
        class="bt_up"
        type="default"
        :plain="true"
        :hairline="true"
        text="调货退货"
        @click="toReturn"
      ></up-button>
    </view>
  </view>
  <view
    class="bt_form"
    v-permission="['shop:scTransfer:sign']"
    v-if="showSelect == 'shop' && type == 2 && optionDet.status == 3"
  >
    <view class="bt_form_k">
      <up-button
        v-if="optionDet.status == 3"
        class="bt_up bt_bl"
        :plain="true"
        :hairline="true"
        text="确认签收"
        @click="showQs"
      ></up-button>
    </view>
  </view>

  <up-popup
    :show="passwordShow"
    round="15"
    mode="center"
    @close="passwordShow = false"
  >
    <view class="pop-content">
      <view class="pop-content-info">
        <view class="title"> 请输入登录密码校验 </view>
        <view class="content-to">
          <up-input
            class="input"
            placeholder="请输入登录密码"
            suffixIcon="map-fill"
            v-model="password"
            suffixIconStyle="color: #909399"
            :type="!isshowpassword ? 'password' : 'text'"
          >
            <!-- eye-off -->
            <template #suffix>
              <up-icon
                @click="isshowpassword = !isshowpassword"
                :name="!isshowpassword ? 'eye-fill' : 'eye-off'"
                color="#86909C"
                size="36rpx"
              ></up-icon>
            </template>
          </up-input>
        </view>
      </view>

      <view class="btns">
        <view class="btns-bt" @click="passwordShow = false">取消</view>

        <view class="btns-qd" @click="qrUpSign">确定</view></view
      >
    </view>
  </up-popup>
  <noWork @connected="connected"> </noWork>
</template>

<style lang="scss" scoped>
@import "index.scss";
</style>
